package com.lzq;

import com.lzq.service.UserManager;
import com.lzq.service.UserManagerImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author 2019/7/25 17:47 by 刘赵强
 **/
public class JdkProxy implements InvocationHandler {

    private Object target;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("invoke 开始");
        Object invoke = method.invoke(target, args);
        System.out.println("invoke 结束");
        return invoke;
    }

    private Object getJdkProxy(Object targetObject){
        this.target = targetObject;
        return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),targetObject.getClass().getInterfaces(),this);
    }

    public static void main(String[] args) {
        JdkProxy jdkProxy = new JdkProxy();
        UserManager user = (UserManager)  jdkProxy.getJdkProxy(new UserManagerImpl());
        user.addUser("a","b");
    }
}
